package pl.us.ij.plugins.segmentation.commands.watershed;

import ij.process.FloatProcessor;
import ij.process.ImageProcessor;
import pl.us.ij.plugins.segmentation.infrastructure.Command;
import pl.us.ij.plugins.segmentation.infrastructure.exceptions.BreakCommandException;

import static pl.us.ij.plugins.segmentation.commands.edges.DilatationCommand.dilatation;
import static pl.us.ij.plugins.segmentation.commands.edges.ErosionCommand.erosion;

/**
 * Created by IntelliJ IDEA.
 * User: Przemek
 * Date: 2010-01-10
 * Time: 21:07:26
 * To change this template use File | Settings | File Templates.
 */
public class OCCOCommand extends Command<OCCOCommandParams> {
    @Override
    protected void processImage(FloatProcessor fp) throws BreakCommandException {

        int maskSize = getParams().getMaskSize();
        int width = fp.getWidth();
        int height = fp.getHeight();

        ImageProcessor ip = fp;

        float[] pixels = (float[]) ip.getPixels();
        float[] pixels1 = (float[]) ip.getPixelsCopy();
        float[] pixels2 = (float[]) ip.getPixelsCopy();
        float[] pixels3 = (float[]) ip.getPixelsCopy();

        dilatation(width, height, maskSize, pixels, pixels1);
        erosion(width, height, maskSize, pixels1, pixels3);
        erosion(width, height, maskSize, pixels3, pixels1);
        dilatation(width, height, maskSize, pixels1, pixels3);

        dilatation(width, height, maskSize, pixels, pixels1);
        erosion(width, height, maskSize, pixels1, pixels2);
        erosion(width, height, maskSize, pixels2, pixels1);
        dilatation(width, height, maskSize, pixels1, pixels2);

        for (int i = 0; i < pixels.length; i++) {
            pixels[i] = 0.5f * pixels2[i] + 0.5f * pixels3[i];
        }

//        getImageProcessor().setPixels(0, (FloatProcessor) ip);
    }
}
